public with sharing class StockingDetailsController {
	/*
	public string  tdId {get;set;}//备货通知单Id 

	public String name { get; set; }//名称
    public String brand { get; set; }//品牌
    public String serial { get; set; }//货号
    public String spec { get; set; } //产品编码

    public String category1 { get; set; } //一级分类
    public String category2 { get; set; }//二级分类
    public String category3 { get; set; }//三级分类
    public String model {get;set;}//型号

    public String msg {get;private set;}//消息
    public Boolean isSuccess {get;private set;}//是否成功

    public List<StockingDetailsBean> oldList {get;set;}//已有
    public List<StockingDetailsBean> searchList {get;set;}//搜索

    public StockingDetails__c std {get;set;}

    public Integer tableHeight {get;set;}//表格高度

    public String sealflag ;//封存标记

    public void init () {
       	tdId = System.currentPageReference().getParameters().get('tdId');
       	std = new StockingDetails__c();
       	this.getStockDetail();
        searchList = new List<StockingDetailsBean>();

         User u = [select Id,SubCompany__c from User where Id=:UserInfo.getUserId()];
          ///获取封存标记
        Map<String,String> cm = getPicklistValues('Contract__c','Corpcode__c');
        sealflag = 'Y-'+cm.get(u.SubCompany__c);


       
    }
      //获得PickList的值
    public  Map<String, String> getPicklistValues(String sObjectName, String sFieldName){
        Map<String, String> picValues= new Map<String, String>();
        Map<String, Schema.SObjectType> sObjectDescribeMap = Schema.getGlobalDescribe();
        if (sObjectDescribeMap.containsKey(sObjectName)) {
            Map<String, Schema.SobjectField> sObjectFieldsMap = 
               sObjectDescribeMap.get(sObjectName).getDescribe().fields.getMap();
            if (sObjectFieldsMap.containsKey(sFieldName)) {
                Schema.DescribeFieldResult sObjectFieldDescribe = sObjectFieldsMap.get(sFieldName).getDescribe();
                List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();
                for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {
                    if(tPicklistEntry.isActive() == true){
                        picValues.put(tPicklistEntry.getLabel(),tPicklistEntry.getValue());
                    }
                 
                }
            } else {
                //TODO 不存在此字段的执行
            }
        } else {
            // TODO 不存在此sObject的执行
        }
        return picValues;
    }

    ///获取 已经存在的明细
    public void getStockDetail(){
    	oldList = new List<StockingDetailsBean>();
    	for(StockingDetails__c sd:[select Id,StockingProduct__c,StockingProduct__r.Brand__c ,StockingProduct__r.NCCode__c,StockingProduct__r.SerialNo__c,CoopType__c,
    	InventoryName__c,Quantity__c,Specification__c,IsSycToNc__c
    	from StockingDetails__c where StockingNotice__c =: tdId]){
    		oldList.add(new StockingDetailsBean(sd));
    	}
    	tableHeight = oldList.size() < 10 ? 150 + oldList.size()*22 : 400;


    }
    ///保存
    public void saveres(){
    	msg = '';
    	List<StockingDetails__c>  stList= new List<StockingDetails__c>();
    	try {

    		for (StockingDetailsBean tdb : oldList) {
    			
    			StockingDetails__c td = new StockingDetails__c();
    			td.StockingProduct__c = tdb.productId;
    			td.Quantity__c = tdb.quantity;
    			//td.Receiver__c = tdb.receiver;
    			//td.ReceivingPhone__c = tdb.receivingPhone;
    			//td.RequestArrivalDate__c = tdb.std.RequestArrivalDate__c;
    			//td.ShippingAddress__c = tdb.shippingAddress;
    			td.StockingNotice__c = tdId;
                td.IsSycToNc__c = tdb.isSycToNc;
                td.CoopType__c = tdb.std.CoopType__c;

    			stList.add(td);

    		}



    	    
    	}
    	catch (Exception e) {
    	 msg = '保存失败!'+ e.getMessage();
    	 isSuccess = false;  
    	 return; 
    	}
    	
    	SavePoint sp = Database.setSavepoint();
    	try {
    		List<StockingDetails__c> deList = [select Id from StockingDetails__c where StockingNotice__c =:tdId];

    		if(stList.size() > 0){
    			if(deList.size() > 0)delete deList;
    			insert stList;
    			msg = '保存成功!';
    			isSuccess = true;
    		}else{
    			msg = '没有数据!';
    			isSuccess = false;
    		}
    	    
    	}
    	catch (Exception e) {
    	    msg = '保存失败!'+ e.getMessage();
	    	isSuccess = false;  
	    	return;  
    	}
    	

    }
    ///交换上下拦
    public void exchange (){
    	List<StockingDetailsBean> temp = new List<StockingDetailsBean>();
    	Set<Id>  existendId= new Set<Id>();
    	for (StockingDetailsBean tdb : oldList) {
    		if(tdb.chk == false){
    			temp.add(tdb);
    			existendId.add(tdb.productId);
    		}
    	}

    	oldList.clear();
    	oldList.addAll(temp);

    	for (StockingDetailsBean tdb : searchList) {
    		if(tdb.chk == true && !existendId.contains(tdb.productId)){
    			
    			tdb.chk = false;
    			oldList.add(tdb.clone());
    		}
    	}
            tableHeight = oldList.size() < 10 ? 150 + oldList.size()*22 : 400;
    }
    ///筛选产品
    public void  searchProduct(){
    	String whereStr = '';
    	searchList = new List<StockingDetailsBean>();
    	if (String.isBlank(name) == false) {
            whereStr += ' and Name like \'%' + name + '%\'';
        }
        if (String.isBlank(brand) == false) {
            whereStr += ' and  Brand__c like \'%' + brand + '%\'';
        }
        if (String.isBlank(serial) == false) {
            whereStr += ' and  SerialNo__c like \'%' + serial + '%\'';
        }
         if (String.isBlank(spec) == false) {
            whereStr += ' and  Spec__c like \'%' + spec + '%\'';
        }
        if (String.isBlank(category1) == false) {
            whereStr += ' and  Category1__c like \'%' + category1 + '\'';
        }
        if (String.isBlank(category2) == false) {
            whereStr += ' and  Category2__c = \'' + category2 + '\'';
        }
         if (String.isBlank(category3) == false) {
            whereStr += ' and  Category3__c = \'' + category3 + '\'';
        }
          if (String.isBlank(model) == false) {
            whereStr += ' and  Model__c  like \'%' + model + '%\'';
        }

      
        List<Product__c> plist = Database.query(this.soqlStr() + whereStr + ' limit 200');

        for(Product__c p:plist){
            if(!(p.SealCompany__c != null && p.SealCompany__c.contains(sealflag)))searchList.add(new StockingDetailsBean(p));
        }
    }
    private String soqlStr() {
        Schema.DescribeSobjectResult m = Product__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' from Product__c where pk_invbasdoc__c != null ';

        return sql;
    }
    public class StockingDetailsBean{
    	public String productId {get;set;}//产品id 
    	public String productName {get;set;}//产品名称
    	public String brand {get;set;}//品牌
    	public String spec {get;set;}//规格
    	public String serialNo {get;set;}//货号
    	public Decimal quantity {get;set;}//数量
        public String code {get;set;}
    	public String receiver {get;set;}//收货人
    	public String receivingPhone {get;set;}//收货人电话
    	public String shippingAddress {get;set;}//收货地址
    	public Date requestArrivalDate {get;set;}//要求到货日期;
        public Boolean isSycToNc ;
    	public Boolean chk {get;set;}//是否选中
    	public StockingDetails__c std {get;set;}//

    	public StockingDetailsBean(Product__c record){
    		productId = record.Id;
    		productName = record.Name;
    		brand = record.Brand__c;
    		spec = record.Spec__c;
    		serialNo = record.SerialNo__c;
            isSycToNc = false;
    		chk = false;
            code = record.NCCode__c;
    		std = new StockingDetails__c();

    	}

    	public StockingDetailsBean(StockingDetails__c record){
    		productId = record.StockingProduct__c;
    		productName = record.InventoryName__c;
    		brand = record.StockingProduct__r.Brand__c;
    		spec = record.Specification__c;
    		serialNo = record.StockingProduct__r.SerialNo__c;
            code = record.StockingProduct__r.NCCode__c;
    		//receiver = record.Receiver__c;
    		//receivingPhone = record.ReceivingPhone__c;
    		quantity = record.Quantity__c ;
    		//shippingAddress = record.ShippingAddress__c;
    		//requestArrivalDate = record.RequestArrivalDate__c;
    		std = new StockingDetails__c();
            std.CoopType__c = record.CoopType__c;
            isSycToNc = record.IsSycToNc__c ;
    		//std.RequestArrivalDate__c = record.RequestArrivalDate__c;




    		chk = false;
    	}

    }
    */


}